/**
 * @Author: wangliuyang
 * @Date: 2021/9/7 14:00
 * @Description:
 */
public class Solution_76 {
    public String minWindow(String s, String t){
        if (t.length() > s.length()){
            return "";
        }
        int[] chars = new int[256];
        int all = 0;    //记录t需要字符数量
        for (int i = 0; i < t.length(); i++) {
            chars[t.charAt(i)]++;
            all++;
        }
        int l = 0;
        int r = 0;
        int start = 0;
        int minLen = Integer.MAX_VALUE;
       while (r < s.length()){
           char c = s.charAt(r);
           if (chars[c]-- > 0){
               all--;
           }
           while (all == 0){
               if (r - l + 1 < minLen){
                   minLen = r - l + 1;
                   start = l;
               }
               char c1 = s.charAt(l);
               if (chars[c1]++ > 0){
                   all++;
               }
               l++;
           }
           r++;
       }
       if (minLen == Integer.MAX_VALUE){
           return "";
       }
       return s.substring(start,start + minLen);
    }
}
